package com.portal.gui.marketmanger;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;
import com.portal.logic.Sale;
import com.portal.logic.SaleDAO;
import com.portal.logic.SerializationUtil;

public class AddSaleScr extends JFrame {

	private JPanel contentPane;
	private JTextField txtSaleName;
	private JTextField txtSaleStart;
	private JTextField txtSaleEnd;
	private JTextField txtSaleCountClients;
	private JTextField txtAgeSegmentStart;
	private JTextField txtAgeSegmentEnd;
	private JTextField txtResidentialArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddSaleScr frame = new AddSaleScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddSaleScr() {
		setTitle("Add New Sale");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 462, 335);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(33, 252, 97, 25);
		contentPane.add(btnBack);
		
		JLabel lblName = new JLabel("Sale Name");
		lblName.setBounds(33, 13, 76, 16);
		contentPane.add(lblName);
		
		JLabel lblNewLabel = new JLabel("Sale Start(YYYY-MM-DD)");
		lblNewLabel.setBounds(33, 42, 151, 16);
		contentPane.add(lblNewLabel);
		
		JLabel lblSaleEndyyyymmdd = new JLabel("Sale End(YYYY-MM-DD)");
		lblSaleEndyyyymmdd.setBounds(33, 71, 151, 16);
		contentPane.add(lblSaleEndyyyymmdd);
		
		JLabel lblCountCustomers = new JLabel("Count Clients");
		lblCountCustomers.setBounds(33, 100, 113, 16);
		contentPane.add(lblCountCustomers);
		
		JLabel lblAgesegmentstart = new JLabel("Age segment start");
		lblAgesegmentstart.setBounds(33, 129, 126, 16);
		contentPane.add(lblAgesegmentstart);
		
		JLabel lblAgeSegmentEnd = new JLabel("Age segment end");
		lblAgeSegmentEnd.setBounds(33, 158, 126, 16);
		contentPane.add(lblAgeSegmentEnd);
		
		JLabel lblResidentialArea = new JLabel("Residential Area");
		lblResidentialArea.setBounds(33, 188, 113, 16);
		contentPane.add(lblResidentialArea);
		
		JLabel lblProduct = new JLabel("Product");
		lblProduct.setBounds(33, 217, 56, 16);
		contentPane.add(lblProduct);
		
		final JComboBox cmbProducts = new JComboBox();
		ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
		List<Product> products = productDAO.selectAll();
		Iterator<Product> iterator = products.iterator();
		while (iterator.hasNext()) {
			Product product = (Product) iterator.next();
			cmbProducts.addItem(product.getName());
		}
		cmbProducts.setBounds(258, 214, 117, 22);
		contentPane.add(cmbProducts);
		
		txtSaleName = new JTextField();
		txtSaleName.setBounds(259, 10, 116, 22);
		contentPane.add(txtSaleName);
		txtSaleName.setColumns(10);
		
		txtSaleStart = new JTextField();
		txtSaleStart.setBounds(259, 39, 116, 22);
		contentPane.add(txtSaleStart);
		txtSaleStart.setColumns(10);
		
		txtSaleEnd = new JTextField();
		txtSaleEnd.setBounds(259, 71, 116, 22);
		contentPane.add(txtSaleEnd);
		txtSaleEnd.setColumns(10);
		
		txtSaleCountClients = new JTextField();
		txtSaleCountClients.setBounds(259, 100, 116, 22);
		contentPane.add(txtSaleCountClients);
		txtSaleCountClients.setColumns(10);
		
		txtAgeSegmentStart = new JTextField();
		txtAgeSegmentStart.setBounds(259, 129, 116, 22);
		contentPane.add(txtAgeSegmentStart);
		txtAgeSegmentStart.setColumns(10);
		
		txtAgeSegmentEnd = new JTextField();
		txtAgeSegmentEnd.setBounds(259, 155, 116, 22);
		contentPane.add(txtAgeSegmentEnd);
		txtAgeSegmentEnd.setColumns(10);
		
		txtResidentialArea = new JTextField();
		txtResidentialArea.setBounds(259, 185, 116, 22);
		contentPane.add(txtResidentialArea);
		txtResidentialArea.setColumns(10);
		
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO add validations to fields
				String saleName = (String)txtSaleName.getText();
				String saleStart = (String)txtSaleStart.getText();
				String saleEnd = (String)txtSaleEnd.getText();
				int saleCountClients = Integer.parseInt(txtSaleCountClients.getText());
				int ageSegmentStart = Integer.parseInt(txtAgeSegmentStart.getText());
				int ageSegmentEnd = Integer.parseInt(txtAgeSegmentEnd.getText());
				String residentialArea = (String)txtResidentialArea.getText();
				String selectedProduct = (String)cmbProducts.getSelectedItem();
				
				SaleDAO saleDAO = new SaleDAO(ConnectionManager.getConnection());
				Product product = null;
				ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
				product = productDAO.selectByName(selectedProduct);
				
				Sale addSale = new Sale();
				addSale.setId(saleDAO.selectMaxId()+1);
				addSale.setName(saleName);
				addSale.setStart(saleStart);
				addSale.setEnd(saleEnd);
				addSale.setCountClients(saleCountClients);
				addSale.setAgeSegmentStart(ageSegmentStart);
				addSale.setAgeSegmentEnd(ageSegmentEnd);
				addSale.setResidentialArea(residentialArea);
				addSale.setProductId(product.getId());
				
				try {
					SerializationUtil.serialize(addSale, "c:\\sale.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				
				saleDAO.insert();
				JOptionPane.showMessageDialog(null, "New Sale was added!");
				
			}
		});
		btnSubmit.setBounds(259, 252, 97, 25);
		contentPane.add(btnSubmit);
	}

}
